package com.fqcar.fqdf.core.http;

import android.content.Context;
import android.net.Uri;


import com.fqdf.framework.android.util.CheckLogicUtil;
import com.fqdf.framework.android.util.DialogUtil;
import com.fqdf.framework.android.util.EnvUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 张枭
 * @version V1.0
 * @date 2017/3/21
 * @desc
 */
public class UrlHelper {

    private static volatile UrlHelper mInstance;
    private static HashMap<String, String> paramValue = new HashMap<>();

    private static HashMap<String, String> cacheUrl = new HashMap<>();

    private String DEFAULT_CITYCODE = "default_citycode";

    public static final String CITY_CODE_KEY = "cityCode";

    private UrlHelper() {
    }

    /**
     * 先进行初始化才能正常使用
     *
     * @param context
     */
//    public static void initInstance(Context context) {
//        if (mInstance == null) {
//            synchronized (UrlHelper.class) {
//                if (mInstance == null) {
//                    mInstance = new UrlHelper();
//                    paramValue.put("os", "android");
//                    paramValue.put("deviceId", EnvUtil.getDeviceId(context));
//                    paramValue.put("deviceType", EnvUtil.getPhoneType(context));
//                    paramValue.put("marketId", EnvUtil.getUMChannelName(context));
//                    paramValue.put("versionId", EnvUtil.getPhoneSysVersion(context));
//                    paramValue.put("appVersion", EnvUtil.getAppVersionCode(context));
//                    paramValue.put("sourceFor", EnvUtil.getSOURCE_FOR(context));
//
//                }
//            }
//        } else {
//            throw new IllegalAccessError("initinstance only call one time");
//        }
//    }
    public static UrlHelper getInstance(Context context) {
//        if (mInstance == null) {
//            throw new IllegalAccessError("initinstance first before call getinstance");
//        }

        if (mInstance == null) {
            synchronized (UrlHelper.class) {
                if (mInstance == null) {
                    mInstance = new UrlHelper();
                    paramValue.put("os", "android");

                    String deviceID = EnvUtil.getDeviceId(context);
                    if (CheckLogicUtil.isEmpty(deviceID)) {
                        DialogUtil.ToastMessage(context, "设备没有权限");
                    }
                    paramValue.put("deviceId", deviceID);
                    paramValue.put("deviceType", EnvUtil.getPhoneType(context));
                    paramValue.put("marketId", EnvUtil.getChannelName(context));
                    paramValue.put("versionId", EnvUtil.getPhoneSysVersion(context));
                    paramValue.put("appVersion", EnvUtil.getAppVersionCode(context));
                    paramValue.put("sourceFor", EnvUtil.getSOURCE_FOR(context));

                }
            }
        }
//        else {
//            throw new IllegalAccessError("initinstance only call one time");
//        }
        return mInstance;
    }

//    /**
//     * 获取url，url是用原始url加上param进行拼接
//     *
//     * @param url      原始url
//     * @param cityCode 城市code
//     * @return 拼接后的url
//     */
    /*public String getAppendUrl(String url, String cityCode) {
        String result = getAppendParam(cityCode);
        if (CheckLogicUtil.isEmpty(url)) {
            return result;
        }

        if (url.contains("?")) {
            return url + "&" + result;
        } else {
            return url + "?" + result;
        }

    }*/

    public String getAppendUrl(String url) {
//        return getAppendUrl(url, null);
        return url;
    }

    /**
     * 根据citycode 来获取拼接后的参数值
     *
     * @param cityCode 城市码
     * @return
     */
    public String getAppendParam(String cityCode) {
        if (CheckLogicUtil.isEmpty(cityCode)) {

            if (!cacheUrl.containsKey(DEFAULT_CITYCODE)) {
                cacheUrl.put(DEFAULT_CITYCODE, generateAppendUrl());
            }
        } else {
            DEFAULT_CITYCODE = cityCode;

            if (!cacheUrl.containsKey(DEFAULT_CITYCODE)) {

                paramValue.put(CITY_CODE_KEY, DEFAULT_CITYCODE);

                cacheUrl.put(DEFAULT_CITYCODE, generateAppendUrl());
            }
        }
        return cacheUrl.get(DEFAULT_CITYCODE);

    }

    /**
     * 生成附加URL参数
     *
     * @return
     */
    private String generateAppendUrl() {
        StringBuffer buffer = new StringBuffer();
        for (Map.Entry<String, String> item : paramValue.entrySet()) {
            buffer.append(item.getKey()).append("=").append(item.getValue()).append("&");
        }
        if (buffer.length() > 0) {
            buffer.setLength(buffer.length() - 1);
        }
        return buffer.toString();
    }

    /**
     * 判断url中是否含有对应参数
     * @param url
     * @param param
     * @return
     */
    public static boolean containParam(String url, String param) {
        if (CheckLogicUtil.isEmpty(url) || CheckLogicUtil.isEmpty(param)) {
            return false;
        }

        Uri uri = Uri.parse(url);
        String value = uri.getQueryParameter(param);
        return !CheckLogicUtil.isEmpty(value);
    }
}
